package com.iou.view;

import java.sql.SQLException;

import com.iou.bean.IOUNote;
import com.iou.bean.IOUUser;
import com.iou.database.DatabaseHelper;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;
import com.vaadin.ui.UI;
import com.vaadin.ui.Window;

/**
 * Simple window showing the "Do you really want to delete this note" message
 * including yes and no buttons.
 */

public class DeleteView {

    // Class variables:

    private Window mywindow;
    private Button yesbutton;
    private Button nobutton;
    private Label areyouwanna;
    private IOUUser user;
    private IOUNote note;
    private InfoView infoikkuna;

    /**
     * Constructor.
     * 
     * @param user
     * @param note
     * @param infoikkuna
     */
    public DeleteView(final IOUUser user, final IOUNote note,
            final InfoView infoikkuna) {
        this.note = note;
        this.user = user;
        this.infoikkuna = infoikkuna;
        AbsoluteLayout windowLayout = new AbsoluteLayout();
        windowLayout.setSizeFull();
        mywindow = new Window("IOU");
        mywindow.center();
        mywindow.setHeight("150px");
        mywindow.setWidth("300px");
        mywindow.setResizable(false);
        mywindow.setModal(true);
        mywindow.setContent(windowLayout);

        UI.getCurrent().addWindow(mywindow);

        // Components' initialization:
        yesbutton = new Button("Yes");
        nobutton = new Button("No");
        areyouwanna = new Label("Do you really wanna delete this note?");

        /*
         * Yes button. Deletes the note from DB and then closes the both
         * (DeleteView and InfoView) windows.
         */
        yesbutton.addClickListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                try {
                    DatabaseHelper.deleteNote(note);
                    Notification.show("Note deleted!",
                            Notification.Type.TRAY_NOTIFICATION);
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                infoikkuna.closeWIndow();
                mywindow.close();
                ListView.refreshList();
                ListView.addToTable();
            }
        });

        // No-button simply closes the window:
        nobutton.addClickListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                mywindow.close();
            }
        });
        windowLayout.addComponent(areyouwanna, "top:10px; left:10px");
        windowLayout.addComponent(nobutton, "bottom:10px; right:10px");
        windowLayout.addComponent(yesbutton, "bottom:10px; right:60px");
    }
}
